/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.filters;

import ca.trader.logger.TraderLogger;
import ca.trader.utils.DebugUtils;
import ca.trader.utils.HttpConsts;
import ca.trader.utils.HttpUtils;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** * This filter redirects to index.jsp if a user is currently logged in . Currently does this for viewprofile and createsale page.   The
 * pages this filter shall work on are specified in the web.xml file
 * @author Ahsan
 */
public class RedirectToIndexIfNotLoggedInFilter implements Filter
{

	 private static final String _className = RedirectToIndexIfNotLoggedInFilter.class.getName();
	 // The filter configuration object we are associated with.  If
	 // this value is null, this filter instance is not currently
	 // configured. 
	 private FilterConfig filterConfig = null;

	 public RedirectToIndexIfNotLoggedInFilter()
	 {
	 }

	 private void doBeforeProcessing(ServletRequest request, ServletResponse response)
		    throws IOException, ServletException
	 {
		  
		  String _methodName = "doBeforeProcessing";
		  
		  HttpServletRequest req = (HttpServletRequest) request;
		  HttpServletResponse resp = (HttpServletResponse) response;
		  
		  TraderLogger.logInfo(_className, _methodName, "Checking if user already authenticated in servlet: " + req.getServletPath());
		  
		  // Check if no user is already authenticated.   if it is, redirect that user to index		  
		  if (!HttpUtils.wasUserAuthenticationSuccess(req.getSession()))
		  {
			   TraderLogger.logInfo(_className, _methodName, "No user already authenticated, redirecting to index");
			   TraderLogger.logInfo(_className, _methodName, "Exiting servlent: " + req.getServletPath());

			   // Redirect to Index
			   HttpUtils.redirect(HttpConsts.INDEX_JSP_PAGE, resp);

		  }

		  
		  TraderLogger.logInfo(_className, _methodName, "User is authenticated, moving forward: " + req.getServletPath());


	 }

	 private void doAfterProcessing(ServletRequest request, ServletResponse response)
		    throws IOException, ServletException
	 {
	 }

	 /**
	  *
	  * @param request The servlet request we are processing
	  * @param response The servlet response we are creating
	  * @param chain The filter chain we are processing
	  *
	  * @exception IOException if an input/output error occurs
	  * @exception ServletException if a servlet error occurs
	  */
	 public void doFilter(ServletRequest request, ServletResponse response,
		    FilterChain chain)
		    throws IOException, ServletException
	 {


		  doBeforeProcessing(request, response);

		  Throwable problem = null;
		  try
		  {
			   chain.doFilter(request, response);
		  }
		  catch (Throwable t)
		  {
			   // If an exception is thrown somewhere down the filter chain,
			   // we still want to execute our after processing, and then
			   // rethrow the problem after that.
			   TraderLogger.logSevere(_className, "doFilter", "Unknown Error", DebugUtils.getStackTraceAsString(t));
		  }

		  doAfterProcessing(request, response);

		  // If there was a problem, we want to rethrow it if it is
		  // a known type, otherwise log it.
		  if (problem != null)
		  {
			   TraderLogger.logSevere(_className, "doFilter", "Unknown Error", DebugUtils.getStackTraceAsString(problem));
			   if (problem instanceof ServletException)
			   {
				    throw (ServletException) problem;
			   }
			   if (problem instanceof IOException)
			   {
				    throw (IOException) problem;
			   }
		  }
	 }

	 /**
	  * Return the filter configuration object for this filter.
	  */
	 public FilterConfig getFilterConfig()
	 {
		  return (this.filterConfig);
	 }

	 /**
	  * Set the filter configuration object for this filter.
	  *
	  * @param filterConfig The filter configuration object
	  */
	 public void setFilterConfig(FilterConfig filterConfig)
	 {
		  this.filterConfig = filterConfig;
	 }

	 /**
	  * Destroy method for this filter
	  */
	 public void destroy()
	 {
	 }

	 /**
	  * Init method for this filter
	  */
	 public void init(FilterConfig filterConfig)
	 {
		  this.filterConfig = filterConfig;
		  if (filterConfig != null)
		  {
		  }
	 }

}
